;(function($,window,document,undefined){

	function Dialog(config){
		var _this=this;

		this.config={
			buttons:null,
			type:"warning",
			delay:null,
			message:null,
			headMessage:null,
			width:"auto",
			height:"auto",
			maskOpacity:null,
			animate:null
		};

		if(config && $.isPlainObject(config)){
			$.extend(this.config,config);
		}else{
			this.isConfig=true;
		}

		this.body=$("body");
		this.mask=$('<div class="g-dialog-container">');
		this.win=$('<div class="dialog-window">');
		this.winHeader=$('<div class="dialog-header"></div>');
		this.contHeadr=$('<div class="dialog-headcont"></div>');
		this.winContent=$('<div class="dialog-content"></div>');
		this.winFooter=$('<div class="dialog-footer">');

		this.creat();
	}

	Dialog.zIndex=10000;

	Dialog.prototype={

		creat:function(){

			var _this_=this,
				config=this.config,
				mask=this.mask,
				win=this.win,
				header=this.winHeader,
				headMsg=this.contHeadr,
				content=this.winContent,
				footer=this.winFooter,
				body=this.body;

			Dialog.zIndex++;
			mask.css({"z-index":Dialog.zIndex});

			if(this.isConfig){
				win.append(header.addClass('loading'));
				
				if(config.animate){
					this.animate();
				}

				mask.append(win);
				body.append(mask);
				$(".dialog-window").css({
					"background":"transparent"
				});
				$(".g-dialog-container .dialog-window").css({
					"box-shadow":"none"
				});

			}else{

				header.addClass(config.type);
				win.append(header);
				if(config.headMessage){
					headMsg.html(config.headMessage);
					win.append(headMsg);

				}

				if(config.message){
					win.append(content.html(config.message));
				}

				if(config.buttons){

					this.ceratButtons(footer,config.buttons);

					win.append(footer);
				}

				if(config.width!="auto"){
					win.width(config.width);
				}
				if(config.height!="auto"){
					win.height(config.height);
				}

				if(config.maskOpacity){
					mask.css({"background":"rgba(0,0,0,"+config.maskOpacity+")"});
				}

				if(config.delay && config.delay!=0){
					setTimeout(function(){
						_this_.close();
					},config.delay);
				}

				if(config.animate){
					this.animate();
				}
				mask.append(win);
				body.append(mask);
			}
		},

		close:function(){
			this.mask.remove();
			// this.mask.hide();
		},

		ceratButtons:function(footer,buttons){
			// console.log(buttons)
			var _this_=this;

			$(buttons).each(function(i){

				var type=this.type ? " class='"+this.type+"'" : "";

				var btnText=this.text ? this.text : "按钮"+(++i);

				var callback=this.callback ? this.callback : null;

				var button=$("<button"+type+">"+btnText+"</button>");

				if(callback){
					button.click(function(){
						var isClose=callback();
						if(isClose!=false){
							_this_.close();
						}
					});
				}else{
					button.click(function(){
						_this_.close();
					});
				}
				footer.append(button);
			});

		},

		animate:function(){
			var _this_=this;
			this.win.css({
				"transform":"scale(0,0)"
			});
			setTimeout(function(){
				_this_.win.css({
					"transform":"scale(1,1)"
				},100);
			})
		}

	};

	// window.Dialog=Dialog;

	$.dialog=function(config){
		return new Dialog(config);
	};

	$.show=function(msg){
		return new Dialog({message:msg,delay:1500});
	};

	$.succeed=function(msg){
		return new Dialog({message:msg,delay:1500,type:"ok"});
	}
	
})(jQuery,window,document);